var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   num:12,
   commits:66,
   style:'font-size:15px; color:#313131; font-weight:normal;',
   poster:'',
   commentsArray:[],
   id:'',
   title:'',
   times:'',
   description:'',
    videoUrl:'http://icon.tongchengyouzhi.com/snsdyvideodownload.mp4',
   showCommit:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
  },
  showError:function(event){
    console.log('视频信息错误：')
    console.log(event.detail.errMsg)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
     wx.request({
       url: app.globalData.baseUrl+'coursecenter/course/detail/'+that.data.id,
       method:'GET',
       header:{
         'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
       },
       success:function(res){
         console.log(res)
         var resData=res.data.data
         try{
           that.setData({
             num:resData.serialNumber,
             title:resData.title,
             times:resData.studyNum,
             commits:resData.commentList.length,
             description:resData.description,
             videoUrl:resData.fileUrl,
             poster:resData.imgUrl
           })
           if(resData.commentList.length<=0){
              that.setData({
                showCommit:true
              })

           }else{
             for (var i = 0; i < resData.commentList.length; i++) {
               var avatarUrl='commentsArray['+i+'].avatarUrl'
               var nickName='commentsArray['+i+'].nickName'
               var content='commentsArray['+i+'].content'
               var date='commentsArray['+i+'].date'
               that.setData({
                 [avatarUrl]:resData.commentList[i].user.avatarUrl,
                 [nickName]:resData.commentList[i].user.nickName,
                 [content]:resData.commentList[i].content,
                 [date]:resData.commentList[i].gmtCreateFormat.substring(0,13)

               })
             }
           }
          
         }catch(err){

         }
       
       },
       fail:function(err){
          wx.showToast({
            title: '获取课程数据失败',
          })
          console.err(err)
       }
     })
  },
  submitComment:function(event){
    // console.log(1111111)
    // console.log(event)
      var content=event.detail.v
     
      var that=this
      wx.request({
        url: app.globalData.baseUrl+'coursecenter/course/detail/'+that.data.id+'/comment',
        method:'POST',
        header:{
          'cookie':'JSESSIONID='+wx.getStorageSync('sessionID'),
          'content-type':'application/x-www-form-urlencoded'
        },
        data:{
          // courseDetailId:that.data.id,
          content:content
        },
        success:function(res){
          console.log('成功 ')
          console.log(res)
          that.onShow();
          that.setData({
            showCommit:false
          })
        },
        fail:function(err){
          console.log('fail')
          console.log(err)
        }
      })
   },
   showVideoLoading:function(event){
     console.log(event)
     wx.showLoading({
       title: '正在加载中...',
     })
   },
   hideVideoLoading:function(event){
     wx.hideLoading()
   },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})